package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.GxClinic;
import com.ruoyi.system.mapper.GxClinicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.GxPatientMapper;
import com.ruoyi.system.domain.GxPatient;
import com.ruoyi.system.service.IGxPatientService;

/**
 * 患者Service业务层处理
 * 
 * @author gx
 * @date 2022-05-07
 */
@Service
public class GxPatientServiceImpl implements IGxPatientService 
{
    @Autowired
    private GxPatientMapper gxPatientMapper;

    @Autowired
    private GxClinicMapper gxClinicMapper;

    /**
     * 查询患者
     * 
     * @param patientId 患者主键
     * @return 患者
     */
    @Override
    public GxPatient selectGxPatientByPatientId(Long patientId)
    {
        return gxPatientMapper.selectGxPatientByPatientId(patientId);
    }

    /**
     * 查询患者列表
     * 
     * @param gxPatient 患者
     * @return 患者
     */
    @Override
    public List<GxPatient> selectGxPatientList(GxPatient gxPatient)
    {
        return gxPatientMapper.selectGxPatientList(gxPatient);
    }

    public List<GxPatient> selectArchiveList(GxPatient gxPatient)
    {
        gxPatient.setPatientStatus("1");
        // 未就诊患者
        List<GxPatient> patients = gxPatientMapper.selectGxPatientList(gxPatient);
        List<GxPatient> archivePatients = new ArrayList<>();
        // 查询未就诊患者中，就诊次数大于等于一次的，说明已归档。
        for (GxPatient patient : patients) {
            Long patientId = patient.getPatientId();
            GxClinic gxClinic = new GxClinic();
            gxClinic.setPatientId(patientId);
            List<GxClinic> clinics = gxClinicMapper.selectGxClinicList(gxClinic);
            // 该患者有就诊信息
            if (clinics.size() >= 1) {
                archivePatients.add(patient);
            }
        }
        return archivePatients;
    }


    /**
     * 新增患者
     * 
     * @param gxPatient 患者
     * @return 结果
     */
    @Override
    public int insertGxPatient(GxPatient gxPatient)
    {
        String uuid = UUID.randomUUID().toString();
        String code = uuid.replace("-","").toUpperCase();
        gxPatient.setPatientNo("DYYKF"+ code);
        gxPatient.setCreateTime(DateUtils.getNowDate());
        return gxPatientMapper.insertGxPatient(gxPatient);
    }

    /**
     * 修改患者
     * 
     * @param gxPatient 患者
     * @return 结果
     */
    @Override
    public int updateGxPatient(GxPatient gxPatient)
    {
        return gxPatientMapper.updateGxPatient(gxPatient);
    }

    /**
     * 批量删除患者
     * 
     * @param patientIds 需要删除的患者主键
     * @return 结果
     */
    @Override
    public int deleteGxPatientByPatientIds(Long[] patientIds)
    {
        return gxPatientMapper.deleteGxPatientByPatientIds(patientIds);
    }

    /**
     * 删除患者信息
     * 
     * @param patientId 患者主键
     * @return 结果
     */
    @Override
    public int deleteGxPatientByPatientId(Long patientId)
    {
        return gxPatientMapper.deleteGxPatientByPatientId(patientId);
    }
}
